package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.UserOauth;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:17
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class UserOauthDao extends BaseDao<UserOauth> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<UserOauth> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<UserOauth> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<UserOauth> list = run.query(sql, new ResultSetHandler<List<UserOauth>>() {
            @Override
            public List<UserOauth> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    UserOauth userOauth = new UserOauth();

                    userOauth.setId(rs.getInt("id"));
                    userOauth.setUserId(rs.getInt("user_id"));
                    userOauth.setAccessToken(rs.getString("access_token"));
                    userOauth.setExpireIn(rs.getString("expire_in"));
                    userOauth.setOauthCode(rs.getString("oauth_code"));
                    userOauth.setOauthType(rs.getInt("oauth_type"));
                    userOauth.setOauthUserId(rs.getString("oauth_user_id"));
                    userOauth.setRefreshToken(rs.getString("refresh_token"));

                    list.add(userOauth);
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  UserOauth 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public UserOauth selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, user_id, access_token, expire_in, oauth_code, oauth_type, oauth_user_id, refresh_token from mto_user_oauth where id = ?";
        UserOauth userOauth = run.query(sql,new ResultSetHandler<UserOauth>() {
            @Override
            public UserOauth handle(ResultSet rs) throws SQLException {
                UserOauth userOauth = null;
                if(rs.next()) {
                    userOauth = new UserOauth();

                        userOauth.setId(rs.getInt("id"));
                        userOauth.setUserId(rs.getInt("user_id"));
                        userOauth.setAccessToken(rs.getString("access_token"));
                        userOauth.setExpireIn(rs.getString("expire_in"));
                        userOauth.setOauthCode(rs.getString("oauth_code"));
                        userOauth.setOauthType(rs.getInt("oauth_type"));
                        userOauth.setOauthUserId(rs.getString("oauth_user_id"));
                        userOauth.setRefreshToken(rs.getString("refresh_token"));
                }
                return  userOauth;
            }
        },params);
        return userOauth;
    }

    /**
    *
    * @param userOauth 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(UserOauth userOauth) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO mto_user_oauth (  ");
        sql.append("  user_id, access_token, expire_in, oauth_code, oauth_type, oauth_user_id, refresh_token ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?,  ?,  ?,  ?,  ?,  ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            userOauth.getUserId(),  
            userOauth.getAccessToken(),  
            userOauth.getExpireIn(),  
            userOauth.getOauthCode(),  
            userOauth.getOauthType(),  
            userOauth.getOauthUserId(),  
            userOauth.getRefreshToken()
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  userOauth
    * @return
    * @throws SQLException
    */
    @Override
    public int update(UserOauth userOauth) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   mto_user_oauth           ");
        sql.append(" SET                    ");
            
            sql.append("   user_id  = ?,  ");
            sql.append("   access_token  = ?,  ");
            sql.append("   expire_in  = ?,  ");
            sql.append("   oauth_code  = ?,  ");
            sql.append("   oauth_type  = ?,  ");
            sql.append("   oauth_user_id  = ?,  ");
            sql.append("   refresh_token  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                userOauth.getUserId(), 
                userOauth.getAccessToken(), 
                userOauth.getExpireIn(), 
                userOauth.getOauthCode(), 
                userOauth.getOauthType(), 
                userOauth.getOauthUserId(), 
                userOauth.getRefreshToken(), 
            userOauth.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from mto_user_oauth where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
